"""
社交互动数据模型
"""
from django.db import models
from django.contrib.auth import get_user_model
from apps.routes.models import RoutePlan

User = get_user_model()


class Post(models.Model):
    """旅游动态"""
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='posts',
        verbose_name='用户'
    )
    content = models.TextField(verbose_name='内容')
    route_plan = models.ForeignKey(
        RoutePlan,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='posts',
        verbose_name='关联路线'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'posts'
        verbose_name = '旅游动态'
        verbose_name_plural = '旅游动态'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} 的动态"


class PostImage(models.Model):
    """动态图片"""
    post = models.ForeignKey(
        Post,
        on_delete=models.CASCADE,
        related_name='post_images',
        verbose_name='动态'
    )
    image = models.ImageField(upload_to='posts/', verbose_name='图片')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'post_images'
        verbose_name = '动态图片'
        verbose_name_plural = '动态图片'

    def __str__(self):
        return f"{self.post.id} 的图片"


class Comment(models.Model):
    """评论"""
    post = models.ForeignKey(
        Post,
        on_delete=models.CASCADE,
        related_name='comments',
        verbose_name='动态'
    )
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='comments',
        verbose_name='用户'
    )
    content = models.TextField(verbose_name='评论内容')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'comments'
        verbose_name = '评论'
        verbose_name_plural = '评论'
        ordering = ['created_at']

    def __str__(self):
        return f"{self.user.username} 的评论"


class Like(models.Model):
    """点赞"""
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='likes',
        verbose_name='用户'
    )
    post = models.ForeignKey(
        Post,
        on_delete=models.CASCADE,
        related_name='likes',
        verbose_name='动态'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'likes'
        verbose_name = '点赞'
        verbose_name_plural = '点赞'
        unique_together = ('user', 'post')
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} 点赞了 {self.post.id}"


